package cn.study.server.handler;

import cn.study.message.GroupJoinRequestMessage;
import cn.study.message.GroupJoinResponseMessage;
import cn.study.server.session.Group;
import cn.study.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @Author Zsz
 * @Date 2022/4/4 22:01
 * @Version 1.0
 **/
@ChannelHandler.Sharable
public class GroupJoinRequestMessageHandler extends SimpleChannelInboundHandler<GroupJoinRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, GroupJoinRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        String username = msg.getUsername();
        Group group = GroupSessionFactory.getGroupSession().joinMember(groupName, username);
        if (group != null) {
            channelHandlerContext.channel().writeAndFlush(new GroupJoinResponseMessage(true, "加入成功！"));
        } else {
            channelHandlerContext.channel().writeAndFlush(new GroupJoinResponseMessage(false, "加入失败！"));
        }

    }
}
